package com.hxzy.shop.utils;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.alibaba.fastjson.JSON;
import com.hxzy.shop.entity.MerchantWxconfig;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建人 ： wq
 * 创建时间 ： 2022/3/2 10:16
 * 功能描述 ： 微信工具类
 */
public class WxUtils {

    public static Map<String, Object> parseBodyXml2Map(String xml) {
        Map<String, Object> map = new HashMap<String, Object>();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml);
            // 获取根节点
            Element rootElt = doc.getRootElement();
            //获取根节点下所有节点
            List<Element> list = rootElt.elements();
            //遍历节点
            for (Element element : list) {
                //节点的name为map的key，text为map的value
                map.put(element.getName(), element.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 获取当前小程序token
     */
    public static String getToken(MerchantWxconfig merchantWxconfig) {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(merchantWxconfig.getAppId());
        config.setSecret(merchantWxconfig.getAppSecret());
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        try {
            return wxMaService.getAccessToken();
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成二维码
     *
     * @param
     * @param accessToken
     * @param <T>
     * @return
     */
    public static <T> String QRCode(T accessToken, T page, T scene) {


        //生成二维码
        Map<String, Object> data = new HashMap<>();
        data.put("scene", scene);
        data.put("page", page);
        data.put("width", 430);
        data.put("is_hyaline", true);
        data.put("auto_color", true);
        String body = JSON.toJSONString(data);

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
        try {

        StringEntity stringEntity = new StringEntity(body);
        stringEntity.setContentType("image/png");
        httpPost.setEntity(stringEntity);
        HttpResponse httpResponse = httpClient.execute(httpPost);

        InputStream inputStream = httpResponse.getEntity().getContent();
        byte[] bytes = IOUtils.toByteArray(inputStream);

        return "data:image/png;base64," + Base64.getEncoder().encodeToString(bytes);

    } catch (Exception e) {
        e.printStackTrace();
    }
        return null;
    }

    public static void main(String[] args) {
       // String card = "522128198902132013";
        //System.out.println(IdcardUtil.getBirthDate(card).toDateStr());
        MerchantWxconfig merchantWxconfig = new MerchantWxconfig();
        merchantWxconfig.setAppId("wx8f308787061b8f6b");
        merchantWxconfig.setAppSecret("35a42e2da7d22c67b58417da62e4faf8");
        String s = QRCode(getToken(merchantWxconfig), "subpages/bargainPurchase/assist","2222222" );


    }
}
